﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FullMotion.LiveForSpeed.InSim.Events;

namespace FGHandler
{
    class lfsplayer
    {
        private RaceTrackConnection _RaceTrackConnection;
        private string _username;
        private byte _connectionId;
        private bool _isAdmin;
        private bool _isRemote;
        private string _playerName;

        RaceTrackPlayer _RaceTrackPlayer;
        


        private TimeSpan _firstSplit;
        private TimeSpan _secondSplit;
        private TimeSpan _thirdSplit;

        private CarInfo _carinfo;

        private RaceTrackPlayerPitStopBegin _RaceTrackPlayerPitStop;


        private RaceTrackPlayerPenalty _RaceTrackPlayerPenalty;
        private RaceTrackPlayerSplitTime _RaceTrackPlayerSplitTime;

        private List<Lap> _LapList = new List<Lap>();

        private bool _spectating;
        private string _teamname;

        private bool _teamCurrent;
        public bool TeamCurrent { get { return this._teamCurrent; } set { this._teamCurrent = value; } }

        public lfsplayer()
        {

        }

        public string Username { get { return _username; } }        

        public string PlayerName { get { return this._playerName; } }

        private byte _playerId;
        public byte PlayerID
        {
            get { return this._playerId; }
            set { this._playerId = value; }
        }
        public byte ConnectionID { 
            get { return this._connectionId; } 
            set { this._connectionId = value; }
        }



        public bool Spectating
        {
            get { return this._spectating; }
            set { this._spectating = value; }
        }

        public string Teamname
        {
            get { return this._teamname; }
            set { this._teamname = value; }
        }

        private byte _HandicapMass = 0;
        private decimal _HandicapIntakeRestriction = 0;
        
        public void RaceTrackPlayer(RaceTrackPlayer RaceTrackPlayer)
        {
            this._playerId = RaceTrackPlayer.PlayerId;
            this._HandicapMass = RaceTrackPlayer.HandicapMass;
            this._HandicapIntakeRestriction = RaceTrackPlayer.HandicapIntakeRestriction;
            this._RaceTrackPlayer = RaceTrackPlayer;
            this.Spectating = false;
        }

        public void RaceTrackConnection(RaceTrackConnection RaceTrackConnection)
        {
            this._username = RaceTrackConnection.UserName;
            this._connectionId = RaceTrackConnection.ConnectionId;
            this._isAdmin = RaceTrackConnection.IsAdmin;
            this._isRemote = RaceTrackConnection.IsRemote;
            this._playerName = RaceTrackConnection.PlayerName;

            this._RaceTrackConnection = RaceTrackConnection;
        }

        public void RaceTrackPlayerSplitTime(RaceTrackPlayerSplitTime RaceTrackPlayerSplitTime)
        {
            if (RaceTrackPlayerSplitTime.Split == 1)
                this._firstSplit = RaceTrackPlayerSplitTime.SplitTime;
            if (RaceTrackPlayerSplitTime.Split == 2)
                this._secondSplit = RaceTrackPlayerSplitTime.SplitTime;
            if (RaceTrackPlayerSplitTime.Split == 3)
                this._thirdSplit = RaceTrackPlayerSplitTime.SplitTime;

            this._RaceTrackPlayerSplitTime = RaceTrackPlayerSplitTime;
        }

        public void RaceTrackPlayerPenalty(RaceTrackPlayerPenalty RaceTrackPlayerPenalty)
        {
            

        }

        public void CarInfo(CarInfo CarInfo)
        {
            this._carinfo = CarInfo;
        }

        public void RaceTrackPlayerPitStop(RaceTrackPlayerPitStopBegin RaceTrackPlayerPitStopBegin)
        {
            this._RaceTrackPlayerPitStop = RaceTrackPlayerPitStopBegin;
            this._pitstopStarted = TimeSpan.FromTicks(DateTime.Now.Ticks);
        }

        public int CurrentLap { get {
            if (_carinfo != null)
                return _carinfo.Lap;
            else
                throw new Exception("No CarInfo.CurrentLap available");
        } }

        public int RacePosition
        {
            get
            {
                if (_carinfo != null)
                    return _carinfo.RacePosition;
                else
                    throw new Exception("No CarInfo.RacePosition available");
            }
        }

        public float Speed
        {
            get
            {
                if (_carinfo != null)
                    return _carinfo.Speed;
                else
                    throw new Exception("No CarInfo.Speed available");
            }
        }

        public void RaceTrackPlayerLap(RaceTrackPlayerLap RaceTrackPlayerLap)
        {
            Lap lap = new Lap(RaceTrackPlayerLap.LapsDone, _firstSplit, _secondSplit, _thirdSplit, RaceTrackPlayerLap.LapTime);
            lap.NumberOfPitStops = RaceTrackPlayerLap.NumberOfPitStops;
            this._LapList.Add(lap);            
        }

        public List<Lap> LapList { get { return this._LapList; } }

        private string _realname = "";
        public string RealName { get { return this._realname; } set { this._realname = value; } }

        private string _nationality = "";
        public string Nationality { get { return this._nationality; } set { this._nationality = value; } }

        private bool _gotteam = false;
        public bool GotTeam { get { return this._gotteam; } set { this._gotteam = value; } }

        private TimeSpan _pitstoptime;
        public TimeSpan PitStopTime
        {
            get { return this._pitstoptime; }
            set { this._pitstoptime = value; }
        }

        private TimeSpan _pitstopStarted;
        public TimeSpan PitStopStarted
        {
            get { return this._pitstopStarted; }
            set { this._pitstopStarted = value; }
        }


        public Decimal HandicapIntakeRestriction { get { return this._HandicapIntakeRestriction;} }
        public byte HandicapMass { get { return this._HandicapMass; } }

        public void Debug()
        {
            Console.WriteLine(string.Format("PLID:{0} ConnID:{1} Username:{2} Lapcount:{3}", PlayerID, ConnectionID, Username, this.LapList.Count));
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2}", PlayerID, ConnectionID, Username, this.LapList.Count);
        }
    }
}
